<?php

namespace app\admin\controller;

use think\Controller;

class Search extends Controller
{

    //页面跳转
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Search')->order('update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Search')->order('update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $tags = $data['data'];

        foreach ($tags as $k=>$v){

            $tags[$k]['tags']= explode(",",$tags[$k]['tags']);

        }

        $return['data'] = $tags;
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }
    //添加搜索
    public function  creatSearch(){
         $page = input('post.page');
         $size = input('post.size');
        $tag = input('post.tags');
        $tags ='';
        foreach ($tag as $k=>$v){
            $tags =$tags.",".$v;
        }
            $data = [
                'title' => input('post.title'),
                'tags' => $tags,
                'browse' => 0,
                'author' => session('username'),
                'weight' => 0
            ];
            $resault = model('Search')->creatSearch($data);
            if ($resault==1){
                $return = $this->page($page,$size,true,"添加成功");
            }else{
                $return = $this->page($page,$size,false,$resault);
            }
               return json($return);

    }
    //删除搜索
    public function deleteSearch(){
        $page = input('delete.page');
        $size = input('delete.size');
        $id = input('delete.id');
        $basesInfo = model('Search')->where('id',$id)->find();
        if (!$basesInfo){
            $return = $this->page($page,$size,false,"查无此条搜索");
        }else{
            $result = model('Search')->where('id',$id)->delete();

            $return = $this->page($page,$size,true,"删除成功");

        }

        return json($return);

    }
    //分类搜索
    public function listeSearch(){
        $size = input('get.size');
        $page = input('get.page');
        $return = $this->page($page,$size,true,"查询成功");

        return json($return);

    }
    //修改搜索
    public function updateSearch(){
        $size = input('put.size');
        $page = input('put.page');

        $data = [
            'title' => input('put.title'),
            'tags' => input('put.tags'),

        ];

            $id = input('put.id');
            $basesInfo =model('Search')->where('id',$id)->find();
            if ($basesInfo){
                $result = model('Search')->updateSearch($id,$data);
                if ($result==1){
                    $return = $this->page($page,$size,true,"更新成功");

                }
                else{
                    $return = $this->page($page,$size,false,$result);
                }
            }else{
                $return = $this->page($page,$size,false,"查无此信息，请仔细核对id");
            }

        return json($return);

    }
    //修改权重
    public function weight()
    {
        $size = input('put.size');
        $page = input('put.page');
        $id = input('put.id');
        $weight = input('put.weight');

        $result = model('Search')->where('id', $id)->update(['weight' => $weight]);
        if ($result) {
            $return = $this->page($page,$size,true,"更新成功");

        }else{
            $return = $this->page($page,$size,false,"跟新失败");
        }
        return json($return);
    }
}
